package cn.itcast.jx.service.impl;

import java.io.Serializable;
import java.util.List;

import cn.itcast.jx.dao.BaseDao;
import cn.itcast.jx.domain.Dept;
import cn.itcast.jx.domain.Module;
import cn.itcast.jx.domain.User;
import cn.itcast.jx.service.ModuleService;
import cn.itcast.jx.util.Page;
import cn.itcast.jx.util.UtilFuns;

public class ModuleServiceImpl implements ModuleService{

	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public Page<Module> findPage(String hql, Page<Module> page,
			Class<Module> entityClass, Object[] params) {
		return baseDao.findPage(hql, page, entityClass, params);
	}

	@Override
	public Module get(Class<Module> entityClass, Serializable id) {
		// TODO Auto-generated method stub
		return baseDao.get(entityClass, id);
	}

	@Override
	public void saveOrUpdate(Module entity) {
		if(UtilFuns.isEmpty(entity.getId())) {
			//新增
			baseDao.saveOrUpdate(entity);
			
		}else {
			//修改
			baseDao.saveOrUpdate(entity);
			
		}
	}

	@Override
	public void delete(Class<Module> entityClass, String[] ids) {
		for (String id : ids) {
			Module module = baseDao.get(entityClass, id);
			module.setState(0);
			baseDao.saveOrUpdate(module);
		}
	}

	@Override
	public List<Module> find(String hql, Class<Module> entityClass,
			Object[] params) {
		// TODO Auto-generated method stub
		return baseDao.find(hql, entityClass, params);
	}
	
	public List<Module> removeModule(List<Module> deptList,Module dept) {
		String hql = "from Module where state=1 and parent.id=?";
		
		List<Module> list= baseDao.find(hql,Module.class, new Serializable[] {dept.getId()});
		
		if(list != null && list.size() != 0) {
			for (Module dept1 : list) {
				if(dept1.getParent().getId().equals(dept.getId())) {
					removeModule(deptList, dept1);
		 			deptList.remove(dept1);
				}
			}
		}
		deptList.remove(dept);
		return deptList;
	}
}
